package com.lkm.market2_0.m_imple.steelyarddata;

import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.javatool.CollectionHelp;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.entity.StallEntity;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.netapi.Api;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.task.AsyncTask2;

import java.util.ArrayList;
import java.util.List;



 class LoadSteelyardInfoTask extends AsyncTask2<String,String,ResponEntity<SteelyardInfo>> {

    public LoadSteelyardInfoTask(ITaskExecutor<String, String, ResponEntity<SteelyardInfo>> taskExecutor, TaskManager mTaskManager, String id) {
//        LoadSteelyardInfoTask.class.getName()
        super(taskExecutor, id, mTaskManager);
    }

    @Override
    protected void doAsyncExecuting(String steelyardMac){
        String json = Api.loadSteelyardInfo(steelyardMac,LoadSteelyardInfoTask.this);

        
        ResponEntity<List<SteelyardInfo> > re=ResponEntity.fromJson(
                json
                ,new com.google.gson.reflect.TypeToken<List<SteelyardInfo>>() {}.getType(),""
        );

        boolean isNew = true;
        json = ExpandConfig.instance.instask(getId(),re,json);
        if (json != null) {
            re = ResponEntity.fromJson(
                    json
                    ,new com.google.gson.reflect.TypeToken<List<SteelyardInfo>>() {}.getType(),""
            );
            isNew = false;
        }

        List<SteelyardInfo> steelyardInfoList = re == null ? null : re.getData();
        if (steelyardInfoList != null) {
            for (SteelyardInfo info : steelyardInfoList) {
                if(info==null)continue;
                info.isNew = isNew;
                if(info.deviceModel==null)
                    info.deviceModel = "";

                if(info.deviceAge == null)
                    info.deviceAge = "";


                if(info.steelyardMac == null){
                    info.steelyardMac = steelyardMac;
                }

                if(info.b_id == null){
                    info.b_id = "";
                }

                if(info.traceurl == null){
                    info.traceurl = "";
                }

                if(info.stallEntity == null){
                    info.stallEntity = new StallEntity();
                }
               if(info.breedTo == null){
                   info.breedTo = new ArrayList<>();
               }
               if(info.orginfo == null){
                   
               }

            }
        }



        ResponEntity<SteelyardInfo > res=new ResponEntity(re);

        SteelyardInfo steelyardInfo = CollectionHelp.getFirst(steelyardInfoList);

        if(steelyardInfo!=null){
            res.setData(steelyardInfo);

            if(!ExpandConfig.instance.isUI()) {
                UpSteelyardRegTask upSteelyardRegTask = new UpSteelyardRegTask();
                upSteelyardRegTask.action();
                upSteelyardRegTask.setParam(steelyardInfo);
                upSteelyardRegTask.execTask();
            }

        }else{
            

            
            steelyardInfo=new SteelyardInfo();
            steelyardInfo.setSteelyardMac(steelyardMac);

            BreedTo breedTo =new BreedTo();
            breedTo.bid=1;
            breedTo.classId=1;
            breedTo.goods_id=1;
            breedTo.goods_name="烧鹅";
            breedTo.id=30;
            breedTo.price=20;
            List<BreedTo> breedTos=new ArrayList<>();

            steelyardInfo.setBreedTo(breedTos);

            StallEntity stallEntity=new StallEntity();
            stallEntity.setAddress("天河区");
            stallEntity.setId(1);
            stallEntity.setName("胡先生");
            stallEntity.setStallName("THH-1档口");
            stallEntity.setStart(2);
            stallEntity.setUid(1);
            stallEntity.setPhone("1312222222");
            steelyardInfo.setStallEntity(stallEntity);
            steelyardInfo.setTraceurl("");
            steelyardInfo.setId("00");

            steelyardInfo.setOrg(-4711188);

            res.setData(steelyardInfo);

            res.setCode(ResponEntity.Code_Success);

        }
        
        complete(res);

    }
}
